package com.huazai.sangeng;

import com.huazai.domain.Author;

import java.util.Optional;

/**
 * <b>主要使用两个方法来避免空指针异常，即为ofNullable的构造方法和在消费的时候的ifPresent的使用。</>
 * @author Seon
 * @date 2022/11/27 15:15
 */
public class OptionalDemo {

    public static void main(String[] args) {
        Optional<Author> authorOptional = getAuthorOptional();
        authorOptional.ifPresent(author -> System.out.println(author.getName()));

    }

    private static Optional<Author> getAuthorOptional() {
        Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        return Optional.ofNullable(author);
    }
}
